from django.db import models


class Menu(models.Model):
    '''一级菜单'''
    title = models.CharField(verbose_name='标题', max_length=225)
    icon = models.CharField(verbose_name='图标', max_length=225)

    def __str__(self):
        return self.title


class Permission(models.Model):
    """
    权限表+二级菜单(部分权限)
    """
    title = models.CharField(verbose_name='标题', max_length=32)
    url = models.CharField(verbose_name='含正则的URL', max_length=128)
    # 可做一级菜单，
    menu = models.ForeignKey(verbose_name='菜单', to='Menu', blank=True, null=True)
    # 用于进入时显示二级菜单，跟二级菜单不能相容
    parent = models.ForeignKey(verbose_name='非菜单权限', to='self', null=True, blank=True,
                               limit_choices_to={'parent__isnull': True})
    name = models.CharField(verbose_name='路由别名', max_length=225, blank=True, null=True)

    def __str__(self):
        return self.title


class Role(models.Model):
    """
    角色
    """
    title = models.CharField(verbose_name='角色名称', max_length=32)
    permissions = models.ManyToManyField(verbose_name='拥有的所有权限', to='Permission', blank=True, null=True)

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """
    用户表
    """
    name = models.CharField(verbose_name='用户名', max_length=32)
    password = models.CharField(verbose_name='密码', max_length=64)
    email = models.CharField(verbose_name='邮箱', max_length=32)
    roles = models.ManyToManyField(verbose_name='拥有的所有角色', to='Role', blank=True, null=True)

    def __str__(self):
        return self.name
